import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/view/main/Login.vue'
import Main from '@/view/main/Main.vue'
import Home from '@/view/main/Home.vue'

const routes = [
  { path: '/', redirect: '/login' },
  { name: 'Login', path: '/login', component: Login },
  { path: '/main', redirect: '/home', component: Main, children: [
    {name: 'Home', path: '/home', component: Home},
    {name: 'Config', path: '/system/config', component: () => import('@/view/system/Config.vue')},
    {name: 'Menu', path: '/system/menu', component: () => import('@/view/system/Menu.vue')},
    {name: 'Button', path: '/system/button', component: () => import('@/view/system/Button.vue')},
    {name: 'Roles', path: '/system/role', component: () => import('@/view/system/Role.vue')},
    {name: 'Users', path: '/system/user', component: () => import('@/view/system/User.vue')},
    {name: 'Dict', path: '/system/dict', component: () => import('@/view/system/Dict.vue')},
    {name: 'Tag', path: '/system/tag', component: () => import('@/view/system/Tag.vue')},
    {name: 'Log', path: '/system/log', component: () => import('@/view/system/Log.vue')},
    {name: 'File', path: '/system/file', component: () => import('@/view/system/File.vue')},
    {name: 'Feedback', path: '/system/feedback', component: () => import('@/view/system/Feedback.vue')},
    {name: 'Mine', path: '/system/mine', component: () => import('@/view/system/Mine.vue')},
    {name: 'Notice', path: '/system/notice', component: () => import('@/view/system/Notice.vue')},
    {name: 'Infor', path: '/information', component: () => import('@/view/infor/Infor.vue')},
    {name: 'Base', path: '/exam/base', component: () => import('@/view/exam/Base.vue')},
    {name: 'Exam', path: '/exam', component: () => import('@/view/exam/Exam.vue')},
    {name: 'ExamLog', path: '/exam/log', component: () => import('@/view/exam/Log.vue')},
    {name: 'GoodsBrand', path: '/goods/brand', component: () => import('@/view/goods/Brand.vue')},
    {name: 'GoodsType', path: '/goods/type', component: () => import('@/view/goods/Type.vue')},
    {name: 'Saved', path: '/goods/saved', component: () => import('@/view/goods/Saved.vue')},
  ]}
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token")
  if (to.path !== '/login' && !token) {
    next('/')
  } else {
    next()
  }
})

export default router
